<?php

namespace App\Admin\Controllers;

use App\Models\PrintDevice;

use Encore\Admin\Admin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Form\Field\Table;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PrintDeviceController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '打印机管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new PrintDevice());
        $grid->model()->orderBy('id', 'desc');

        $grid->disableExport();
        $cate = \request('cate');

        $type =  DB::table('type')->where('type',$cate)->pluck('name','id')->toArray();


        $grid->filter(function($filter) use ($type){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->equal('type', '打印类型')->select(['1' => '商品', '2' => '订单']);
            $filter->equal('cate', '商品类型')->select(['1' => '开蚌', '2' => '饰品','3'=>'成品']);


        });

        $grid->column('id', __('Id'));

        $grid->column('type', __('打印类型'))->using(['1' => '商品', '2' => '订单']);
        $grid->column('cate', __('商品类型'))->using(['1' => '开蚌', '2' => '饰品','3'=>'成品']);


        $grid->column('device_code', __('设备编号'));



        $grid->column('created_at', __('创建时间'));
        $states = [
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'primary'],
            'off' => ['value' => 2, 'text' => '禁用', 'color' => 'default'],
        ];
        $grid->column('status','禁用状态')->switch($states);
        //$grid->column('updated_at', __('Updated at'));

        $grid->actions(function($actions){

            $actions->disableView();
            $actions->disableDelete();

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Good::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('商品名称'));
        $show->field('product', __('规格属性'));
        $show->field('description', __('商品简介'));
        $show->field('img', __('商品图片'))->image();
        $show->field('old_price', __('原价'));
        $show->field('new_price', __('优惠价'));
        $show->field('cost_price', __('成本价'));
        $show->field('stock', __('库存'));
        $show->field('detail_img', __('详情'))->unescape();
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new PrintDevice());


        $form->select('type', __('打印类型'))->options(['1' => '商品', '2' => '订单'])->required();
        $form->select('cate', __('商品类型'))->options(['1' => '开蚌', '2' => '饰品','3'=>'成品'])->required();

        $form->text('device_code', __('设备编号'))->required();


        $states = [
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
            'off' => ['value' => 2, 'text' => '禁用', 'color' => 'danger'],
        ];

        $form->switch('status', __('状态'))->states($states)->default('1');
        $form->footer(function ($footer) {

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });



        return $form;
    }




}
